/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.taglib;

import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.references.ReferenceService;
import opiam.admin.faare.struts.service.StrutsService;

import org.apache.struts.taglib.TagUtils;

import java.util.Collections;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;


/**
 * This tag allows to load either an operators list (Struts service)
 * or a search reference list (References service).
 *
 */
public class ReferenceElementDefineTag extends BodyTagSupport
{
    /** List identifier. */
    private String id;

    /** List type: reflist or operator. */
    private String type;

    /** List name. */
    private String name;

    /** Tag body. */
    private String body;

    /**
     * Saves the body content of this tag (if any), or throw a JspException
     * if the value was already defined.
     *
     * @return see BodyTagSupport
     *
     * @throws JspException if the value was already defined
     */
    @Override
	public int doAfterBody() throws JspException
    {
        if (bodyContent != null)
        {
            body = bodyContent.getString();

            if (body != null)
            {
                body = body.trim();
            }

            if (body.length() < 1)
            {
                body = null;
            }
        }

        return (SKIP_BODY);
    }

    /**
     * Sets the list name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Returns the list identifier.
     * @return identifier
     */
    @Override
	public String getId()
    {
        return id;
    }

    /**
     * Sets the identifier.
     * @param aid The identifier to set
     */
    @Override
	public void setId(String aid)
    {
        this.id = aid;
    }

    /**
     * Returns the type.
     * @return reflist or operator
     */
    public String getType()
    {
        return type;
    }

    /**
     * Sets the type.
     * @param atype The type to set: reflist or operator
     */
    public void setType(String atype)
    {
        this.type = atype;
    }

    /**
     * Retrieves the required property and exposes it as a scripting variable.
     *
     * @return see BodyTagSupport
     * @throws JspException if a JSP exception has occurred
     */
    @Override
	public int doEndTag() throws JspException
    {
        List value = null;

        try
        {
            if (type.compareToIgnoreCase("operator") == 0)
            {
                value = StrutsService.getOperatorsList(name);
            }
            else
            {
                UserContext userContext = (UserContext) TagUtils.getInstance().lookup(pageContext,
                        "sessionContext", "userContext", "session");

                if (userContext == null)
                {
                    System.out.println(" *** userContext = NULL");
                }

                value = ReferenceService.getReferenceList(name, userContext)
                                        .getReferenceList();
            }
        }
        catch (Exception e)
        {
            value = Collections.EMPTY_LIST;
        }

        pageContext.setAttribute(id, value, PageContext.PAGE_SCOPE);

        // Continue processing this page
        return (EVAL_PAGE);
    }
}
